package com.henrybit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.henrybit.annotation.Overload;
import com.henrybit.entity.UserInfo;
import com.henrybit.exception.DBException;
import com.henrybit.util.Constant;
import com.henrybit.util.DBFactory;

/**
 * 用户Dao<p>
 * @author henrybit
 * @version 1.0
 */
public class UserInfoDao extends BaseDao<UserInfo> {

	/**查询基础SQL*/
	private static String queryPre = "select * from user_info where 1=1";
	/**分页默认大小*/
	private static int pageSize = 6;
	/**分页编号*/
	private static int pageNo = 1;
	
	@Override
	public List<UserInfo> query(UserInfo t) throws DBException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Overload
	public List<UserInfo> query(UserInfo t, int page) throws DBException {
		List<UserInfo> list = new ArrayList<UserInfo>();
		String sql = prepareQuerySql(t);
		Connection con = DBFactory.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.getResultSet();
			while(rs.next()) {
				UserInfo userInfo = getUserInfo(rs);
				list.add(userInfo);
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DBException(e, Constant.DB_QUERY_USER_INFO_ERROR, "准备用户查询SQL执行异常");
		} finally {
			try {
				con.close();
			} catch(SQLException e) {
				throw new DBException(e, Constant.DB_CONNECTION_CLOSE_ERROR, "关闭数据库连接异常");
			}
		}
		return list;
	}
	
	@Override
	public void insert(UserInfo t) throws DBException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean update(UserInfo t) throws DBException {
		// TODO Auto-generated method stub
		return false;
	}
	
	private String prepareQuerySql(UserInfo t) {
		int id = t.getId();
		String name = t.getName();
		String email = t.getEmail();
		StringBuilder sql = new StringBuilder(queryPre);
		if(id > 0) {
			sql.append(" and id=").append(id);
		}
		if(name != null && !name.equals("")) {
			sql.append(" and name like '%").append(name).append("%'");
		}
		if(email != null && !email.equals("")) {
			sql.append(" and email='").append(email).append("'");
		}
		return sql.toString();
	}
	
	private UserInfo getUserInfo(ResultSet rs) throws DBException {
		UserInfo userInfo = new UserInfo();
		try {
			int id = rs.getInt("id");
			String email = rs.getString("email");
			String ip = rs.getString("ip");
			String name = rs.getString("name");
			String phone = rs.getString("phone");
			int sex = rs.getInt("sex");
			userInfo.setId(id);
			userInfo.setEmail(email);
			userInfo.setIp(ip);
			userInfo.setName(name);
			userInfo.setPhone(phone);
			userInfo.setSex(sex);
		} catch (Exception e) {
			throw new DBException(e, Constant.DB_USERINFO_PREPARE_DATA_ERROR, "封装用户信息类异常");
		}
		return userInfo;
	}
}
